<?php
defined('BASEPATH') OR exit('No direct script access allowed');




class Hunter extends MY_Controller {

    public $poolnub1=0;
    public $poolnub2=0;


    public function __construct() {
        parent::__construct();
        if($this->userinfo['group']!=1&&$this->userinfo['group']!=3){
            $this->groupjunp($this->userinfo['group']);
        }


    }

    //主页

    /**
     *
     */
    public function index()	{
        $this->address='测试员首页';
        $this->load->view('hunter/head');
        $this->load->view('hunter/left');
        $this->load->model('Jd_task_jd_list_model');
        $a= $this->Jd_task_jd_list_model->getmonthdata($this->user_id);
        $days=explode("-",date('Y-m-d'));
        $days=intval($days[2]);
        $a['avg']=round($a['all']/$days,2);
        $this->load->view('hunter/index',$a);
    }




    public function paylog(){

        $this->address='测试员资金详情';
        $this->load->view('hunter/head');
        $this->load->view('hunter/left');
        $this->load->view('hunter/hunterpaylog');


    }


    /**
     * 京东接单页面
     */
    public  function  getjdtask(){
        $this->address='京东接单';
        $this->load->view('hunter/head');
        $this->load->view('hunter/left');
        $redis=$this->MyRedis();
        $this->poolnub1=$redis->get('poolnub1');


        $awz=$this->session->awz?$this->session->awz:0;
        $data['awz']=$awz;
        $this->load->view('hunter/getjdtask',$data);
    }

    public function getpddtask(){
        $this->address='拼多多接单';
        $this->load->view('hunter/head');
        $this->load->view('hunter/left');
        $awz=$this->session->awz?$this->session->awz:0;
        $redis=$this->MyRedis();
        $this->poolnub2=$redis->get('poolnub2');
        $data['awz']=$awz;
        $this->load->view('hunter/getpddtask',$data);
    }




    public function pddtasklist(){
        $this->address='拼多多已完成';
        $this->load->view('hunter/head');
        $this->load->view('hunter/left');
        $this->load->view('hunter/pddfinishlist');

    }


    /**
     * 获取任务池中的任务
     */
    public function getjdtaskaction($type=1){
        $epage = $this->input->post('epage');
        $page = $this->input->post('page');
        $awz = $this->input->post('awz')?$this->input->post('awz'):"";
        $this->load->model('Jd_task_jd_list_pool_model');
        if($epage>20){
            $epage=20;
        }
        if($this->group!=3 && $this->group!=1){
            $msg['code']=1;
            $msg['msg']='你不是测试员无法获取任务';
            $this->sendjsonmsg($msg);
        }
        if(empty($epage)){
            $epage=3;
        }
        $this->user_id= $this->session->user_id;
        $where="hunter_id=$this->user_id   AND `status` =1 AND type=$type";
        $list=  $this->Jd_task_jd_list_pool_model->pageData($where,"*",$page,100);


       // echo $this->db->last_query();die;
        if(!empty($list['list'])){//如果查询到有在进行的任务直接返回当前任务
            $list['epage']=1;
            $list['page']=$page;
            foreach ($list['list'] as $id=>$v){
                $list['list'][$id]['money']=$v['price']*$v['number'];
                if($v['type']==1){
                    $list['list'][$id]['linkid']=0;
                    $subject='%.*?item.jd.com\\/(\d{0,20})%is';
                    $result= preg_match($subject,$v['link'],$row);
                    if($result){
                        $list['list'][$id]['linkid']=$row[1];
                    }
                }
                $str=explode( "/",$v['picurl']);
                if(  isset($str[1]) && $str[1]=='pic'){
                    $list['list'][$id]['picurl']=base_url()."public/uploads". $v['picurl'];
                }
            }
            if(!isset( $this->session->awz) ||$this->session->awz==0 ){
                $this->session->awz=count( $list['list']);
            }
            $this->sendjsonmsg($list);
        }
        $this->db->trans_begin();
        $list= $this->Jd_task_jd_list_pool_model->HunterGetTask($epage,"",$type);
        $list['epage']=$epage;
        $list['page']=$page;
        if(!empty($list['list'])){
            $idlist=array();
            foreach ($list['list'] as $id=>$v){
                $idlist[]=$v['id'];
                $list['list'][$id]['real_price']="";
            }            $result=$this->Jd_task_jd_list_pool_model->HunterAccept($this->user_id,$idlist,$awz);
            if(!$result){
                $this->db->trans_rollback();
                $msg['code'] = 1;
                $msg['msg'] = '获取任务失败 请重新再试';
                $this->sendjsonmsg($msg);
            }
            $this->db->trans_commit();
            $this->getjdtaskaction($type);//调用自己搜索更新过后的数据
        }
        if(!isset( $this->session->awz) ||$this->session->awz==0 ){
            $this->session->awz=count( $list['list']);
        }
        if(empty($list['list'])){
            $msg['code']=1;
            $msg['msg']='暂无任务';
            $this->sendjsonmsg($msg);
        }

        $this->sendjsonmsg($list);
    }


    public function getpddtaskaction(){
        $this->getjdtaskaction(2);
    }


    /**
     *
     * 测试员完成任务页面
     */
    public function jdfinishlist(){
        $this->address='京东接单';
        $this->load->view('hunter/head');
        $this->load->view('hunter/left');
        $this->load->view('hunter/jdfinishlist');
    }


    /**
     * 获取已完成的任务（京东）
     */
    public function getjdfinishlist($type=1){
        $page = $this->input->post('page');
        $epage = $this->input->post('epage');
        if(empty($epage)){
            $epage=10;
        }
        $this->load->model('Jd_task_jd_list_model');
        $where=" hunter_id=".$this->user_id." AND status=2 AND `type`=$type";

        $orderid=$this->input->post('orderid')?$this->input->post('orderid'):"";
        $awz=$this->input->post('awz')?$this->input->post('awz'):"";

        $shop_name=$this->input->post('shop_name')?$this->input->post('shop_name'):"";

        $date=date('Y-m-d');

        if($orderid){
            $where.=" AND orderid='$orderid'";
        }
        if($awz){
            $where.="  AND awz='$awz'";
        }
        if($shop_name){
            $where.="  AND shop_name like '%$shop_name%'";
        }

        if($date){
            $data1=date('Y-m-d 00:00:00',strtotime($date));
            $data1=strtotime($data1);
            $data2=date('Y-m-d 23:59:59',strtotime($date));
            $data2=strtotime($data2);
            $where.=" AND finishtime>$data1 AND  finishtime<$data2";
        }
        $order['id']="DESC";

        $a=  $this->Jd_task_jd_list_model->pageData($where,"*",$page,$epage);
        $a['epage']=$epage;
        $a['page']=$page;
        foreach ($a['list'] as $id=>$v){
            if($v['status']==0){
                $a['list'][$id]['finishtime']="";
                $a['list'][$id]['status']="未完成";
                $a['list'][$id]['account']="";
                $a['list'][$id]['real_price']="";
                $a['list'][$id]['orderid']="";
            }else{
                $a['list'][$id]['status']="完成";
                $a['list'][$id]['finishtime']= date("Y-m-d H:i:s",$v['finishtime']);
            }
        }
        $this->sendjsonmsg($a);
    }


    public function getpddfinishlist(){
        $this->getjdfinishlist(2);
    }


    /**
     * 导出测试员当天详细记录
     */
    public function finishlistexcel($type=2,$date=""){
        $page = 1;
        $epage = 10000;
        $this->load->model('Jd_task_jd_list_model');
        $where=" hunter_id=".$this->user_id." AND status=2 AND `type`=$type";
        $orderid=$this->input->post('orderid')?$this->input->post('orderid'):"";
        $awz=$this->input->post('awz')?$this->input->post('awz'):"";
        $date=$this->input->post('date')?$this->input->post('date'):date('Y-m-d');

        if($orderid){
            $where.=" AND orderid='$orderid'";
        }
        if($awz){
            $where.="  AND awz='$awz'";
        }
        if($date){
            $data1=date('Y-m-d 00:00:00',strtotime($date));
            $data1=strtotime($data1);
            $data2=date('Y-m-d 23:59:59',strtotime($date));
            $data2=strtotime($data2);
            $where.=" AND finishtime>$data1 AND  finishtime<$data2";
        }
        $order['shop_name']="DESC";


        $select="id,finishtime,shop_name,number,keyword,price,orderid,awz,sku,hunter_name,type,real_price";
        $a=  $this->Jd_task_jd_list_model->pageData($where,$select,$page,$epage);

        $a['epage']=$epage;
        $a['page']=$page;
        foreach ($a['list'] as $id=>$v){
            $a['list'][$id]['finishtime']= date("Y-m-d H:i:s",$v['finishtime']);
            if($v['type']==1){
                $a['list'][$id]['type']="京东";
            }else{
                $a['list'][$id]['type']="拼多多";
            }
        }
        $str[]='ID';
        $str[]='完成时间';
        $str[]='店铺名';
        $str[]='每单数量';
        $str[]='关键字';
        $str[]='录入价格';
        $str[]='订单号';
        $str[]='AWZ';
        $str[]='SKU';
        $str[]='测试员';
        $str[]='类型';
        $str[]='实际付款';
        $this->excel_out($str, $a['list'],"测试员今日订单记录");
    }



    /**
     * 已完成任务修改
     */
    public function  JdEdite(){
        $row= $this->input->post();
        $id=$row['id'];
        $this->load->model('Jd_task_jd_list_model');
        $result=$this->Jd_task_jd_list_model->getonebyid($id);
        if(!isset($result['hunter_id'])){
            $msg['code']=1;
            $msg['msg']="不存在该订单";
            $this->sendjsonmsg($msg);
        }
        if($result['status']!=2){
            $msg['code']=1;
            $msg['msg']="该订单还未完成";
            $this->sendjsonmsg($msg);
        }
        if($result['hunter_id']!=$this->user_id){
            $msg['code']=2;
            $msg['msg']="你不是该订单的测试员禁止修改";
            $this->sendjsonmsg($msg);
        }
        if($result['orderid']==$row['orderid'] && $result['real_price']==$row['real_price'] && $result['awz']==$row['awz']){
            $msg['code']=2;
            $msg['msg']="参数未变";
            $this->sendjsonmsg($msg);
        }

        $a=$this->Jd_task_jd_list_model->updaterealmoney($id,$row);
        if($a){
            $newrow['user_id']=$this->user_id;
            $newrow['oldprice']=$result['real_price'];
            $newrow['oldorderid']=$result['orderid'];
            $newrow['newprice']=$row['real_price'];
            $newrow['neworderid']=$row['orderid'];
            $newrow['date']=date('Y-m-d');
            $newrow['type']=1;
            $newrow['logid']=$id;
            $newrow['addtime']=time();
            $this->load->model('Jd_hunter_log_model');
            $this->Jd_hunter_log_model->addlog($newrow);
            $this->load->model('Jd_task_jd_model');
            $this->Jd_task_jd_model->updatetaskmoney($result['taskid']);
            $this->sendjsonmsg();
        }


    }



    /**
     * 测试员完成任务
     */
    public function finnish(){
        $project = $this->input->post('project')?$this->input->post('project'):1;
        if(empty($project)){
            $msg['code'] = 1;
            $msg['msg'] = '缺少项目标示';
            $this->sendjsonmsg($msg);
        }
        $orderid = $this->input->post('orderid');
        if(strlen($orderid)>23){
            $msg['code'] = 1;
            $msg['msg'] = '订单错误';
            $this->sendjsonmsg($msg);
        }

        $this->project=$project;
        $this->db->trans_begin();

        $result= $this->hunterfinishjd($project);

        if(!$result){
            $this->db->trans_rollback();
            $msg['code'] = 1;
            $msg['msg'] = '任务提交失败';
            $this->sendjsonmsg($msg);
        }
        $this->db->trans_commit();

        $redis=$this->MyRedis(); //更新任务池中的任务数
        $redis->update('poolnub'.$project,-1);
        $msg['code'] = 0;
        $msg['msg'] = '任务完成';
        $this->sendjsonmsg($msg);
    }


    /**
     * 提交完成的任务
     * @return bool
     */
    public function hunterfinishjd($project=1){
        $real_price = $this->input->post('real_price');
        $id = $this->input->post('id');
        $orderid = $this->input->post('orderid');
        $awz = $this->input->post('awz');

        $this->load->model('Jd_task_jd_list_pool_model');
        $row=$this->input->post();

        $row['team']=$this->userinfo['team']="无团队";
        $row['username']=$this->userinfo['username'];


        $result= $this->Jd_task_jd_list_pool_model->finishtask($this->session->user_id,$row);

        unset( $row['team']);
        unset( $row['username']);
        if(!$result){
            return false;
        }
        $r=$this->checktaskend($row);//商户主任务更新
        if(!$r) {
            return false;
        }
        $row['task_nub_finnish']=1;
        $r=$this->UpdataUserinfo($row,$this->user_id);//
        if(!$r) {
            return false;
        }

        $row=array();
        $row['money']=$real_price;
        $row['log_id']=$orderid;
        $row['user_id']=$this->user_id;
        $r=$this->moneyupdate($row,4,$id);
        if(!$r) {
            return false;
        }
        $r= $this->huntertotal($row);
        if(!$r) {
            return false;
        }
        if(isset($this->session->awz)){
            $this->session->awz=$this->session->awz-1;
            if($this->session->awz==0){
                unset($this->session->awz);
            }
        }
        return true;
    }







    /**
     * 计算测试员的总计
     */
    public function huntertotal($row){
        foreach ($row as $id=>$v){
            if($id!='user_id' && $id!="money"){
                unset($row[$id]);
            }
        }
        $this->load->model('Jd_hunter_total_day_model');
        $resutl=$this->Jd_hunter_total_day_model->getupodate($row);
        if($resutl){
            $this->load->model('Jd_hunter_total_month_model');
            $resutl=$this->Jd_hunter_total_month_model->getupodatemoth($row);
        }
        return $resutl;
    }

    /**
     * 检查和更新任务进度
     */
    public function checktaskend($row){
        $this->load->model('Jd_task_jd_list_pool_model');
        $row=$this->input->post();
        $result= $this->Jd_task_jd_list_pool_model->getonebyid($row['id']);
        $this->load->model('Jd_task_jd_model');
        return  $this->Jd_task_jd_model->updatetask($result['taskid']);
    }


    /**
     *
     * 替换任务
     */
    public function repalcetask($type=1){
        $epage = $this->input->post('epage');
        $page = $this->input->post('page');
        $awz = $this->input->post('awz');

        $this->load->model('Jd_task_jd_list_pool_model');
        $this->user_id= $this->session->user_id;
        $row=  $this->Jd_task_jd_list_pool_model->pageData(array('compare'=>array('hunter_id'=>$this->user_id,'status'=>1)),"*",1,100);
        if(empty($row['list'])){
          $msg['code'] = 1;
            $msg['msg'] = '没有任务可以重置';
            $this->sendjsonmsg($msg);
        }
        $idstr=array();
        $shopstr=array();
        foreach ($row['list'] as $id=>$v){
            $idstr[]=$v['id'];
            $shopstr[]=$v['shop_name'];
        }
        $a=  $this->Jd_task_jd_list_pool_model->HunterGetTask($epage,$shopstr,$type);
        if($a['list']){
            $this->Jd_task_jd_list_pool_model->replacetask($idstr);
        }
        $a['epage']=$epage;
        $a['page']=$page;
        if(!empty($a['list'])){
            $idlist=array();
            $listid=array();
            foreach ($a['list'] as $id=>$v){
                $idlist[]=$v['id'];
                $listid[]=$v['listid'];
            }
            $this->Jd_task_jd_list_pool_model->HunterAccept($this->user_id,$idlist,$awz);
            if(!isset( $this->session->awz) ||$this->session->awz==0 ){
                $this->session->awz=count($a['list']);
            }

            $this->sendjsonmsg($a);
        }else{
            $a['status']=1;
            $a['message']="暂无合适任务";

            $this->sendjsonmsg($a);
        }

    }







    public function getpaylog(){
        $page = $this->input->post('page');
        $epage = $this->input->post('epage');
        if(empty($epage)){
            $epage=10;
        }
        $this->load->model('Jd_pay_log_model');
        $username = $this->input->post('username')? $this->input->post('username'):"";
        $date1 = $this->input->post('date1')? $this->input->post('date1'):"";
        $date2 = $this->input->post('date2')? $this->input->post('date2'):"";
        $_sql=" where  user_id= ".$this->user_id;
        if($date1){
            $date1=strtotime($date1);
            $_sql.=" AND  p1.addtime>$date1 ";
        }
        if($date2){
            $date2=strtotime($date2);
            $_sql.=" AND p1.addtime>$date2 ";
        }
        if(!empty($username)){
            $_sql.=" AND  p2.username= '$username'";
        }
        $order['id']="DESC";
        $_select = " p1.*,p2.username";
        $_order = " order by p1.id desc ";
        $sql = "select SELECT from `jd_pay_log` as p1
                left join  `jd_users` as p2  on   p1.user_id=p2.id                  
				 SQL ORDER LIMIT";
        $_data = array("sql" => $sql,
            "_sql" => $_sql,
            "_select" => $_select,
            "_order" => $_order,
            "limit" => "",
            "page" => $page,
            "epage" => $epage);

        $this->load->model('Jd_task_jd_model');
        $a = $this->Jd_task_jd_model->get_server_list($_data);
        foreach ($a['list'] as $id=>$v){
            $a['list'][$id]['addtime']=date('Y-m-d H:i:s',$v['addtime']);
            switch ($v['type']){
                case 1 : $a['list'][$id]['type']='充值';break;
                case 2 : $a['list'][$id]['type']='发布任务';break;
                case 3 : $a['list'][$id]['type']='推荐奖励';break;
                case 4 : $a['list'][$id]['type']='测试员刷单';break;
                case 5 : $a['list'][$id]['type']='兑换金币';break;
                case 6 : $a['list'][$id]['type']='后台调整';break;
            }
        }
        $this->sendjsonmsg($a);

    }

}
